import 'dart:async';
import 'dart:ffi';

import 'package:device_calendar/device_calendar.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_crashlytics/firebase_crashlytics.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:intl/date_symbol_data_local.dart';
import 'package:logger/logger.dart';
import 'package:photo_frame/firebase_options.dart';
import 'package:photo_frame/net/http_manager.dart';
import 'package:photo_frame/provider/app_trans_asset_provider.dart';
import 'package:photo_frame/provider/frames_provider.dart';
import 'package:photo_frame/provider/schedule_provider.dart';
import 'package:photo_frame/push/FCM.dart';
import 'package:photo_frame/router/router_context.dart';
import 'package:photo_frame/router/router_mapping.dart';
import 'package:photo_frame/user/user_manager.dart';
import 'package:photo_frame/utils/a_logger.dart';
import 'package:photo_frame/utils/global_shared_preferences.dart';
import 'package:photo_frame/utils/localization_util.dart';
import 'package:provider/provider.dart';

import 'provider/users_provider.dart';

void main() {
  //[\u4e00-\u9fa5]
  runZonedGuarded(() async {
    WidgetsFlutterBinding.ensureInitialized();
    await Firebase.initializeApp(
      options: DefaultFirebaseOptions.currentPlatform,
    );

    FlutterError.onError = FirebaseCrashlytics.instance.recordFlutterFatalError;
    await GlobalSharedPreferences.instance.init();
    await UserManager.instance.init();
    await HttpManager.instance.init();
    await ScheduleViewModel.init();
    await UsersViewModel.init();
    initializeDateFormatting();
    await FCM().init();
    runApp(App());
  }, (dynamic error, dynamic stack) {
    print(stack);
  });
}

class App extends StatelessWidget {
  static late BuildContext globalCxt;

  @override
  Widget build(BuildContext context) {
    Future.value("").whenComplete(() {
      globalCxt = RouterContext.instance.navigatorKey.currentContext!;
    });
    return MultiProvider(
        providers: [
          ChangeNotifierProvider.value(value: AppAssetTransProvider()),
          FramesProvider.value(),
          ScheduleProvider.value(),
          UsersProvider.value()
        ],
        child: MaterialApp(
          theme: ThemeData(
            colorScheme: ColorScheme.fromSeed(seedColor: Colors.white),
          ),
          onGenerateRoute: routeGenerator,
          // initialRoute: "/selectFrame",
          // initialRoute: "/entry",
          // initialRoute: "/verificationCode",
          navigatorKey: RouterContext.instance.navigatorKey,
          builder: EasyLoading.init(builder: FToastBuilder()),
          supportedLocales: const <Locale>[
            Locale('en'),
            Locale("de"),
            Locale("es"),
            Locale("fr"),
            Locale("it"),
            Locale("ja"),
            Locale("pt"),
            Locale("ru"),
            Locale("zh")
          ],
          localizationsDelegates: AppLocalizations.localizationsDelegates,
          localeResolutionCallback: (locale, supportedLocales) {
            for (var supportedLocale in supportedLocales) {
              if (supportedLocale.languageCode == locale?.languageCode) {
                return supportedLocale;
              }
            }
            return const Locale('en');
          },
        ));
  }
}
